1657B - XY Sequence - CodeForces Solution


greedy

Please click on ads to support us..

Python Code:

t = int(input())
while t:
    t -= 1
    n,B,x,y = list(map(int, input().split()))
    n += 1
    dat = [0]*n
    dat[0]= 0
    s = 0
    for i in range(1,n):
        dat[i] = dat[i-1] + x
        if dat[i] > B:
            dat[i] = dat[i-1] - y
        s += dat[i]
    print(s)

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
#define For(i, s, e) for (ll i = s; i < e; i++)
#define FOR(i, s, e) for (ll i = s; i <= e; i++)
#define FORD(i, s, e) for (ll i = s; i >= e; i--)
#define pb push_back
#define vii vector<ll>
#define makep make_pair
#define vpll vector<pair<ll, ll>>
#define fi first
#define se second
#define sii set<ll>
#define pii pair<int, int>
#define rev(c) reverse(c.begin(), c.end())
#define sortf(c) sort(c.begin(), c.end())
#define sortd(c) sort(c.begin(), c.end(), greater<int>())
#define test()   \
    int test;    \
    cin >> test; \
    while (test--)
#define fast()                        \
    ios_base::sync_with_stdio(false); \
    cin.tie(0);                       \
    cout.tie(0);
#define sqr(n) (n) * (n)
#define len(s) (int)(s.length())
#define re0 return 0
#define re return

int const N2 = 1e4 + 7;
int const N = 1e6 + 7;
int const inf = 1e18 + 7;
int const mod = 1e9 + 7;
char const nl = '\n';

// ! ¸,ø¤º°`°º¤ø,¸¸,ø¤º° [ нvмegy ] °º¤ø,¸¸,ø¤º°`°º¤ø,¸ roadтoнυe

signed main()
{
    fast();

    test() { 
        int n, B, x, y; 
        cin >> n >> B >> x >> y; 
        int sum = 0; 
        int cur = 0, pre = 0; 
        FOR(i, 1, n) { 
            if (pre + x <= B) cur = pre + x;
            else cur = pre - y; 
            sum += cur;
            pre = cur;
        }
        cout << sum << nl;
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1334B - Middle Class
260C - Balls and Boxes
1554A - Cherry
11B - Jumping Jack
716A - Crazy Computer
644A - Parliament of Berland
1657C - Bracket Sequence Deletion
1657B - XY Sequence
1009A - Game Shopping
1657A - Integer Moves
230B - T-primes
630A - Again Twenty Five
1234D - Distinct Characters Queries
1183A - Nearest Interesting Number
1009E - Intercity Travelling
1637B - MEX and Array
224A - Parallelepiped
964A - Splits
1615A - Closing The Gap
4C - Registration System
1321A - Contest for Robots
1451A - Subtract or Divide
1B - Spreadsheet
1177A - Digits Sequence (Easy Edition)
1579A - Casimir's String Solitaire
287B - Pipeline
510A - Fox And Snake
1520B - Ordinary Numbers
1624A - Plus One on the Subset
350A - TL